"use strict";
this.PopupOpener = function () {};

this.PopupOpener.prototype = (function () {
    var init, thread, onConfigurationLoaded, links, config, initLinks,
            getEventHandler, onRedraw, global;

    global = this;

    init = function (webLayout) {};

    thread = function (webLayout) {};

    onConfigurationLoaded = function (webLayout, configuration) {
        if (configuration[this.NAME] === undefined) {
            return;
        }
        links = [];
        config = configuration[this.NAME];
        initLinks(webLayout);
    };

    getEventHandler = function (webLayout, link) {
        return function (e) {
            global.open(link.href, '_blank', '');
            webLayout.preventDefault(e);
        };
    };

    onRedraw = function (webLayout, details) {
        initLinks(webLayout);
    };

    initLinks = function (webLayout) {
        var popupLinks, i, j, added;
        popupLinks = webLayout.xPath(config);
        for (i = popupLinks.length - 1; i >= 0; i -= 1) {
            added = false;
            for (j = links.length - 1; j >= 0; j -= 1) {
                if (links[j] === popupLinks[i]) {
                    added = true;
                }
            }
            if (!added) {
                webLayout.addEventListener(popupLinks[i], 'click',
                        getEventHandler(webLayout, popupLinks[i]));
                links.push(popupLinks[i]);
            }
        }
    };

    return {
        NAME: 'PopupOpener',
        init: init,
        thread: thread,
        onConfigurationLoaded: onConfigurationLoaded,
        onRedraw: onRedraw
    };
}());